package org.sxp.common.utils.datax.reader;


import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Maps;
import org.sxp.common.utils.AeyeReflectionUtil;
import org.sxp.common.utils.SpringContextUtils;
import org.sxp.modules.datasource.entity.DatabaseDO;
import org.sxp.modules.datasource.service.DatabaseService;
import org.sxp.modules.dataworks.dto.SyncDTO;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * mysql reader 构建类
 *
 * @author zhouhongfa@gz-yibo.com
 * @ClassName MysqlReader
 * @Version 1.0
 * @since 2019/7/30 23:07
 */
public class MongoDbReader extends BaseReader implements IDataxReader {

    public MongoDbReader(SyncDTO syncDTO) {
        super(syncDTO);
    }

    @Override
    public String getName() {
        return "mongodbreader";
    }

    @Override
    public Map<String, Object> build() {
        DatabaseService databaseService = SpringContextUtils.getBean(DatabaseService.class);
        DatabaseDO jobDatasource = databaseService.getById(syncDTO.getSourceDb());
        Map<String, Object> readerObj = Maps.newLinkedHashMap();
        readerObj.put("name", getName());
        Map<String, Object> parameterObj = Maps.newLinkedHashMap();

        parameterObj.put("userName", jobDatasource.getUsername());
        parameterObj.put("userPassword", jobDatasource.getPassword());
        parameterObj.put("address", StrUtil.split(jobDatasource.getUrl(), ","));
        parameterObj.put("dbName", jobDatasource.getDbName());
        parameterObj.put("collectionName", syncDTO.getSourceTable());
        parameterObj.put("column", syncDTO.getSourceField());
        //判断是否有where
        if (StrUtil.isNotBlank(syncDTO.getWhereSql())) {
            parameterObj.put("query", syncDTO.getWhereSql());
        }
        readerObj.put("parameter", parameterObj);
        return readerObj;
    }
}
